package com.beiding.utils;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

public class AuthorityUtils {



/*

    //构建权限树

    static {

        //添加基础权限.部分权限管理员可以理解为完全权限管理员在某些行为上的分身

        */
/*********** 顶级管理员admin ***********//*

        GrantedAuthority admin = create("admin");
        registerAuthority(admin,null);

        */
/*********** 客户机管理员.仅有权限管理客户机 ***********//*

        GrantedAuthority admin_client = create("admin_client");
        registerAuthority(admin_client, admin);

        //客户机的增删改查
        registerAuthority(create("admin_client_put"),admin_client);
        registerAuthority(create("admin_client_delete"),admin_client);
        registerAuthority(create("admin_client_update"),admin_client);
        registerAuthority(create("admin_client_get"),admin_client);


    }
*/


    public static GrantedAuthority create(String authority) {
        authority = authority.toUpperCase();
        if (!authority.startsWith("ROLE_"))
            authority = "ROLE_" + authority;
        return new SimpleGrantedAuthority(authority);
    }

/*
    public static final GrantedAuthority USER = create("user");
*/

    /*//添加权限
    private static void registerAuthority(GrantedAuthority authority, GrantedAuthority directSupAuthority) {
        AuthorityTree.Node added = new AuthorityTree.Node(authority);
        if (directSupAuthority!=null)
            added.setSupAuthority(directSupAuthority);
        else {
            added.level = 0;
        }
        AuthorityTree.addNode(added);
    }


    //补足权限
    public static void completeAuthorities(Set<GrantedAuthority> authorities)
    {
        //查找最高权限集合
        Set<GrantedAuthority> maxAuthorities = AuthorityTree.maxAuthorities(authorities);
        //遍历最高权限集合,将最高权限的子权限赋予当前用户
        maxAuthorities.forEach(
            it->authorities.addAll(AuthorityTree.getSubAuthorities(it))
        );
    }


    */
    /*
        检查用户是否具有操作指定权限的权限的权限.比如对另一个用户赋予/收回某个权限
        权限只能向下操作
    */
  /*  public static boolean canOperate(Set<GrantedAuthority> authorities, GrantedAuthority authority) {
        AuthorityTree.Node node = AuthorityTree.toAuthorityNode(authority);
        while (node!=null) {
            node = AuthorityTree.toAuthorityNode(node.supAuthority);
            if (node!=null && authorities.contains(node.authority)) {
                return true;
            }
        }
        return false;
    }

    *//*
        权限树

        >. User权限是基础权限,任何角色都不具备对User权限的管理权
        >. 权限树中不包含User权限
        >. 除了admin外其他的管理员都具有User权限
        >. 权限是向下包含的
        >. 高级权限拥有者可以管理低级权限拥有者

        >. 权限树是用来描述权限关系的树结构
     *//*
    private static class AuthorityTree
    {
        //权限节点
        final static Map<GrantedAuthority, Node> nodeMap = new HashMap<>();

        //添加一个权限节点
        static void addNode(Node node)
        {
            if (!nodeMap.containsKey(node.authority))
                nodeMap.put(node.authority, node);
        }


        //权限转换为权限节点
        static Node toAuthorityNode(GrantedAuthority authority) {
            //在这里同时检验权限是否有效
            return nodeMap.get(authority);
        }

        //查找最高权限,一个用户可能拥有平级的多个权限
        static Set<GrantedAuthority> maxAuthorities(Set<GrantedAuthority> authorities) {

            //最大权限制.权限等级和数值是相反的
            int maxLevel = Integer.MAX_VALUE;
            Set<Node> nodes = new HashSet<>();
            for (GrantedAuthority authority : authorities)
            {
                Node node = toAuthorityNode(authority);
                if (node!=null && node.level < maxLevel) {
                    maxLevel = node.level;
                    nodes.add(node);
                }
            }
            Set<GrantedAuthority> r = new HashSet<>();
            for (Node node: nodes)
            {
                if (node.level == maxLevel) {
                    r.add(node.authority);
                }
            }
            return r;
        }

        //获取当前权限的所有子权限
        static Set<GrantedAuthority> getSubAuthorities(GrantedAuthority target) {
            Set<GrantedAuthority> r = null;
            //深度搜索
            Node node = toAuthorityNode(target);
            if (node!=null)
            {
                r=new HashSet<>();
                Set<GrantedAuthority> subs = node.subAuthorities;
                if (subs!=null) {
                    r.addAll(subs);
                    for (GrantedAuthority authority:subs)
                    {
                            r.addAll(getSubAuthorities(authority));
                    }
                }
            }
            return r;
        }

        static class Node
        {
            int level;
            GrantedAuthority authority;
            GrantedAuthority supAuthority;
            Set<GrantedAuthority> subAuthorities = new HashSet<>();
            private Node(GrantedAuthority authority) {
                this.authority = authority;
            }
            void setSupAuthority(GrantedAuthority supAuthority) {
                this.supAuthority = supAuthority;
                Node sup = toAuthorityNode(supAuthority);
                this.level = sup.level + 1;
                sup.addSubAuthority(this.authority);
            }
            private void addSubAuthority(GrantedAuthority subAuthority) {
                subAuthorities.add(subAuthority) ;
            }

        }
    }
*/

}

